/* common function
 * histort:
 *      2022.12.14 base function
 */

#ifndef _COMMON_H_
#define _COMMON_H_

#include <iostream>
#include <chrono>
#include <string>

#define IN
#define OUT
#define INOUT

#define UNUSED(expr) \
  do {               \
    (void)(expr);    \
  } while (0)

#define SAFE_DELETE(p) \
  if (p) {             \
    delete (p);        \
    (p) = nullptr;     \
  }

class MeasureTime {
public:
  explicit MeasureTime(const std::string &tag): tag_(tag),
      time_start_(std::chrono::steady_clock::now()) { }
  ~MeasureTime() {
    time_end_ = std::chrono::steady_clock::now();
    std::chrono::microseconds cost_time =
        std::chrono::duration_cast<std::chrono::microseconds>(
        time_end_ - time_start_);
    std::cout << "[time] " << tag_ << " cost time ["
        << cost_time.count() << "]us" << std::endl;
  }

private:
  std::string tag_;
  std::chrono::steady_clock::time_point time_start_;
  std::chrono::steady_clock::time_point time_end_;
};

#define TIME_COST_START(str)  { MeasureTime mt(str);
#define TIME_COST_END         }

#endif  // endif _COMMON_H_
